package com.small.nacos.config.server.utils;

import small.common.core.constants.CommonConstants;
import small.common.core.util.StringUtil;
import com.small.nacos.sys.env.EnvUtil;

import java.io.File;

/**
 * @Author zhoujin
 * @Date 2022/11/23 19:39
 */
public class DiskUtil {

    static final String TAG_DIR = File.separator + "data" + File.separator + "tag-data";

    static final String TENANT_TAG_DIR = File.separator + "data" + File.separator + "tag-beta-data";

    /**
     * 获取配置文件
     *
     * @param dataId
     * @param group
     * @param tenant
     * @return
     */
    public static File targetFile(String dataId, String group, String tenant, String configType) {
        File file = null;
        if (StringUtil.isBlank(tenant)) {
            file = new File(EnvUtil.getNacosHome(), TAG_DIR);
        } else {
            file = new File(EnvUtil.getNacosHome(), TENANT_TAG_DIR);
            file = new File(file, tenant);
        }
        file = new File(file, group);

        File dataFile = new File(file, dataId);
        if (!dataFile.exists() && StringUtil.isNotBlank(configType)) {
            dataFile = new File(file, dataId + CommonConstants.DOT + configType);
        }
        return dataFile;
    }
}
